package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysuser.dao.SysUserDao;
import com.talkweb.newproject.admin.sysuser.dto.SysUserDTO;
import com.talkweb.newproject.admin.sysuser.service.SysUserService;
import com.talkweb.newproject.api.model.admin.SysUserParam;
import com.talkweb.newproject.api.model.common.Response;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import com.talkweb.newproject.api.model.shiro.LoginInfoVo;
import com.talkweb.newproject.api.model.shiro.UserInfo;
import com.talkweb.newproject.api.model.shiro.UserInfoParam;
import com.talkweb.newproject.common.utils.EncryptUtil;
import com.talkweb.newproject.common.utils.MapperUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;

/**
 * Created by qsf on 2019/4/22.
 */
@RestController
@RequestMapping(value="/api/sysUser")
@Slf4j
public class SysUserRest {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    MapperUtil mapperUtil;

    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method=RequestMethod.POST)
    public ResponseResult<SysUserDTO> querySysUserByPrimaryKey(@PathVariable("id") String id) throws Exception {
        ResponseResult<SysUserDTO> result = new ResponseResult<>();
        try{
            SysUserDTO sysUserDTO = sysUserService.querySysUserByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysUserDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/getSysUserList",method=RequestMethod.POST)
    public Object querySysUserList(@RequestBody SysUserParam obj) throws Exception {
        return sysUserService.searchSysUser(obj);
    }

    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysUserDTO>  queryLike(@RequestBody SysUserDTO obj) throws Exception {
        ResponseResult<SysUserDTO> result = new ResponseResult<>();
        try{
            SysUserDTO dto = sysUserService.queryLikeSysUser(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(dto);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }


    }

    @RequestMapping(value="/getUserInfoByLoginName",method = RequestMethod.POST)
    public ResponseResult<UserInfo> getUserInfoByLoginName(@RequestBody UserInfoParam userInfoParam) throws Exception {
        ResponseResult<UserInfo> result = new ResponseResult<>();
        try{
            UserInfo userInfo = null;
            SysUserDTO dto= new SysUserDTO();
            dto.setLoginName(userInfoParam.getLoginName());
            SysUserDTO sysUserDTO = sysUserService.getUserInfoByLoginName(dto);
            userInfo = mapperUtil.map(sysUserDTO,UserInfo.class);
            result.setStatus(Status.SUCCESS);
            result.setData(userInfo);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    public ResponseResult create(@RequestBody SysUserDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysUserService.inserSysUser(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 修改数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method = RequestMethod.POST)
    public ResponseResult update(@RequestBody SysUserDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysUserService.updateSysUser(obj);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 根据id删除对象
     * @param ids
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/delSysUserByUserId",method = RequestMethod.POST)
    public ResponseResult delSysUserByUserId(String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(ids!=null || !"".equals(ids)) {
                sysUserService.deleteSysUserByPrimaryKey(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 修改密码
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/checkOldPassWord",method = RequestMethod.POST)
    public ResponseResult checkOldPassWord(@RequestParam("LoginName") String loginName,@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            SysUserDTO dto = new SysUserDTO();
            dto.setLoginName(loginName);
            SysUserDTO sysUserDTO = sysUserService.queryLikeSysUser(dto);
            if(sysUserDTO!=null && sysUserDTO.getLoginName()!= null && !"".equals(sysUserDTO.getLoginName())) {
                String oldPasswordHash = EncryptUtil.encryptPsw(sysUserDTO.getPassword());
                if(!oldPasswordHash.equals(sysUserDTO.getPassword())){
                    result.setMessage("原始密码不正确");
                    result.setStatus(Status.FAILD);
                }else {
                    sysUserDTO.setPassword(EncryptUtil.encryptPsw(newPassword));
                    sysUserService.updateSysUser(sysUserDTO);
                    result.setStatus(Status.SUCCESS);
                    result.setMessage("修改成功");
                }
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("该用户名不存在");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    @ApiOperation(value="根据username查询用户信息",notes = "返回结果:SUCCESS:200;FALED:500",httpMethod = "POST")
    @RequestMapping(value="/getLoginInfoByUserName",method = RequestMethod.POST)
    public ResponseResult<UserInfo> getLoginInfoByUserName(@RequestParam SysUserParam obj) throws Exception {
        ResponseResult<UserInfo> result = new ResponseResult();
        try{
            UserInfo userInfo = null;
            SysUserDTO sysUserDTO = new SysUserDTO();
            sysUserDTO.setLoginName(obj.getLoginName());
            SysUserDTO dto=  sysUserService.getUserInfoByLoginName(sysUserDTO);

            userInfo = mapperUtil.map(dto,UserInfo.class);
            result.setStatus(Status.SUCCESS);
            result.setData(userInfo);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage());
            result.setStatus(Status.FAILD);
            return result;
        }

    }


    @RequestMapping(value="/getLoginInfoByUserId",method = RequestMethod.POST)
    public ResponseResult<LoginInfoVo> getLoginInfoByUserId(@RequestParam String userId)throws Exception {
        ResponseResult<LoginInfoVo> result  = new ResponseResult<>();
        try {
            LoginInfoVo loginInfoVo=sysUserService.getLoginInfoByUserId(userId);
            result.setData(loginInfoVo);
            result.setStatus(Status.SUCCESS);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }
}
